﻿class QuizPlayer extends PlayerBase {
    private data: any;
    private quizArr: any[];
    private quizIndex: number;
    private playGround: egret.Sprite;

    public play(): void {
        this.playGround.removeChildren();
        var quiz: any = this.quizArr[this.quizIndex];
        switch (quiz.type) {
            case 0:
                {
                    this.crtQuiz = quiz;
                    this.checkRect = this.crtQuiz.rect;
                    this.hintSnd = this.crtQuiz.snd;
                    var cutt = this.getRes(this.crtQuiz.src);
                    var quizcut: egret.Bitmap = new egret.Bitmap(cutt);

                    this.texture = new egret.Sprite();

                    this.texture.addChild(quizcut);
                    quizcut.x = -quizcut.width / 2;
                    quizcut.y = -quizcut.height / 2;

                    this.texture.x = this.checkRect.x + quizcut.width / 2;
                    this.texture.y = this.checkRect.y + quizcut.height / 2;
                    this.playGround.addChild(this.texture);
                    this.texture.visible = false;
                    this.playGround.addChild(this.texture);

                    this.wrongSnd = this.crtQuiz.wrongSnd;
                    this.rightSnd = this.crtQuiz.rightSnd;

                    this.touchEnabled = true;
                    this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onQuizInteractive, this);

                    this.playSnd(this.hintSnd);
                    break;
                }

            default:
                {
                    break;
                }
        }
    }

    private playSnd(_url: string): void {

       /* if (this.snd)
            this.snd.stop();*/

        if (_url) {
            /*
            this.snd = this.getRes(_url);
            if (this.snd)
                this.snd.play();*/
            //GlobalAudio.play(_url);

            SoundManager.playSound(QNService.getFullUrl(_url), null);

        }
    }

   // private snd: egret.Sound;

    private cover: egret.Bitmap;

    private playFeedback(): void {
        this.playGround.addChildAt(this.msk, 0)
        this.texture.visible = true;
        var tween = egret.Tween.get(this.texture);
        tween.to({ scaleX: 1.2, scaleY: 1.2 }, 500);
        tween.to({ scaleX: 1, scaleY: 1 }, 500);

        setTimeout(() => { this.feedBackComp() }, 1001);
    }

    private feedBackComp(): void {
        this.playGround.removeChildren();

        this.playNext();
    }

    private msk: egret.Shape;

    public stop(): void {
    }

    public pause(): void {

    }

    public resume(): void {

    }

    public dispose(): void {
        /*if (this.snd) {
            this.snd.stop();
        }*/
        SoundManager.stopSound();
        //GlobalAudio.stop();
    }

    private wrongSnd: string;
    private rightSnd: string;
    private hintSnd: string;

    private texture: egret.Sprite;

    private crtQuiz: any;
    private checkRect: egret.Rectangle;

    protected onQuizInteractive(event: egret.TouchEvent): void {
        if (this.checkRect.contains(event.localX, event.localY)) {
            this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onQuizInteractive, this);
            this.playSnd(this.rightSnd);
            this.playFeedback();
        } else {
            this.playSnd(this.wrongSnd);
            this.playWrongFeedback();
        }
    }

    private pausePosition: Number = 0;

    private playNext(): void {
        this.quizIndex++;
        if (this.quizIndex >= this.quizArr.length) {
            this.finish();
        } else {
            this.play();
        }
    }

    private playWrongFeedback(): void {
        //egret.Tween.get(this.cover).to();
    }

    public initData(obj: PaintLoader): void {
        super.initData(obj);

        this.data = this.loader.quizData;
        this.quizArr = this.data.quizArr;

        this.msk = new egret.Shape();
        this.msk.graphics.beginFill(0, .4);
        this.msk.graphics.drawRect(0, 0, 1024, 768);
        this.msk.graphics.endFill();

        this.cover = new egret.Bitmap(this.getRes(this.paint.cover));
        this.addChild(this.cover);

        this.playGround = new egret.Sprite();
        this.addChild(this.playGround);

        this.quizIndex = 0;
    }
}